/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.web30.semantic.core.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import org.web30.semantic.core.model.CrawlerService;
import org.web30.semantic.core.model.MetaRepository;
import org.web30.semantic.core.model.ResourceRepository;
import org.web30.semantic.nntp.ArticleRepository;
import org.web30.semantic.nntp.MessageAnalyzer;
import org.web30.semantic.sesame.SesameRepository;

/**
 *
 * @author xiazy
 */
public class Web30Crawler implements CrawlerService {

    private Map resourceMap = Collections.synchronizedMap(new HashMap());
    private Map metaContainerMap = Collections.synchronizedMap(new HashMap());
    private boolean stop = false;
    private ArrayList tasks = new ArrayList();

    public synchronized void setStop(boolean flag) {
        this.stop = flag;
    }

    public synchronized boolean isStop() {
        return this.stop;
    }

    public void addMetaRepository(MetaRepository rep) {
        this.metaContainerMap.put(rep.getURI(), rep);
    }

    public void removeMetaRepository(String uri) {
        this.metaContainerMap.remove(uri);
    }

    public void addResourceRepository(ResourceRepository rep) {
        this.resourceMap.put(rep.getURI(), rep);
    }

    public void RemoveResourceRepository(String uri) {
        this.resourceMap.remove(uri);
    }

    public void start() {
        Iterator it = this.resourceMap.keySet().iterator();
        while (!this.isStop()) {            
            while (it.hasNext()) {
                ResourceRepository rep = (ResourceRepository) this.resourceMap.get(it.next());
                CrawlerTask temp = new CrawlerTask(rep, this.metaContainerMap);
                this.tasks.add(temp);
                (new Thread(temp)).start();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                return;
            }
        }
    }

    public void stop() {
        this.setStop(true);
    }

    public static void main(String[] args) {
        ArticleRepository nntp = new ArticleRepository("nntp://localhost:7500/trash", "zhenyu.xia@oracle.com", "eds321");
        nntp.setLastId("<1263347929_175@newsgroup.com>");
        nntp.setResourceAnalyzer(new MessageAnalyzer());
        SesameRepository rdf = new SesameRepository("http://www.web30core.org/openrdf-sesame/repositories/rdftest/statements");
        rdf.setProxy("www-proxy.jp.oracle.com", 80);
        Web30Crawler crawler = new Web30Crawler();
        crawler.addResourceRepository(nntp);
        crawler.addMetaRepository(rdf);
        crawler.start();
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                break;
            }
        }
        return;
    }
}
